package com.sddz.pacs.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sddz.pacs.dto.JsonResult;
import com.sddz.pacs.dto.PageModelParams;
import com.sddz.pacs.entity.SysStation;
import com.sddz.pacs.entity.SysUser;
import com.sddz.pacs.entity.SysUserRole;
import com.sddz.pacs.mapper.SysStationMapper;
import com.sddz.pacs.service.SysStationService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.sddz.pacs.util.ShiroUtils;
import com.sddz.pacs.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 系统服务站信息表 服务实现类
 * </p>
 *
 * @author 时代电子
 * @since 2018-09-07
 */
@Service
public class SysStationServiceImpl extends ServiceImpl<SysStationMapper, SysStation> implements SysStationService {
    @Autowired
    SysStationService sysStationService;
    @Override
    public PageInfo<SysStation> selectListByCondition(PageModelParams params){
            PageHelper.startPage(params.getPageNum(), params.getPageSize());
            EntityWrapper<SysStation> ew = new EntityWrapper<>();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
              //获取当前登陆场对象
            SysUser sysUser= ShiroUtils.getUserEntity();
            if (sysUser != null){
                if (sysUser.getState().equals("00")){
                    if (params.getCondition() !=null){
                        if(!StringUtils.isEmpty(params.getCondition().get("stationName"))){
                            ew.like("STATION_NAME", params.getCondition().get("stationName").toString());
                        }
                        if (StringUtils.isNotEmpty(params.getCondition().get("startDate")) && StringUtils.isNotEmpty(params.getCondition().get("endDate"))){
                            try {
                                ew.between("CREATE_DATE",formatter.parse(params.getCondition().get("startDate")+""),formatter.parse(params.getCondition().get("endDate")+""));
                            } catch (ParseException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }else{
                    if (sysUser.getState().equals("0")){
                        if (params.getCondition() !=null){
                            if(!StringUtils.isEmpty(params.getCondition().get("stationName"))){
                                ew.like("STATION_NAME", params.getCondition().get("stationName").toString());
                            }
                            if (StringUtils.isNotEmpty(params.getCondition().get("startDate")) && StringUtils.isNotEmpty(params.getCondition().get("endDate"))){
                                try {
                                    ew.between("CREATE_DATE",formatter.parse(params.getCondition().get("startDate")+""),formatter.parse(params.getCondition().get("endDate")+""));
                                } catch (ParseException e) {
                                    e.printStackTrace();
                                }
                            }
                            if(sysUser.getState().equals("0")){
                                ew.eq("STATE",sysUser.getState());
                            }
                        }
                    }
                }

            }


             List<SysStation> stationList=this.baseMapper.selectList(ew);
             return new PageInfo<>(stationList);
    }
    //
    @Override
    public SysStation queryStationByStationName(String stationName) {
        EntityWrapper<SysStation> ew = new EntityWrapper<>();
        ew.eq("STATION_NAME",stationName);
        return sysStationService.selectOne(ew);
    }
    @Override
    public Integer queryStationByStationNames(String stationName){
        EntityWrapper<SysStation> ew = new EntityWrapper<>();
        ew.eq("STATION_NAME",stationName);
        Integer count = this.baseMapper.queryStationByStationNames(ew);
        return count;
    }

}
